package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttg.model.mapper.QualityManageTypeRuleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.po.QualityManageLadderRulePO;
import com.ttg.model.po.QualityManageTypeRulePO;
import com.ttg.model.pojo.QualityManageLadderRule;
import com.ttg.model.pojo.QualityManageTypeRule;
import com.ttg.web.dao.QualityManageTypeRuleDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 质量管理类别配置(TblQualityManageTypeConfig) daoImpl
 *
 * @author chenyou
 * @datetime 2025-08-18 14:50:36
 */
@Service
public class QualityManageTypeRuleDaoImpl extends ServiceImpl<QualityManageTypeRuleMapper, QualityManageTypeRule> implements QualityManageTypeRuleDao {

    @Override
    public List<QualityManageTypeRule> getByDimType(Integer dimType) {
        LambdaQueryWrapper<QualityManageTypeRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualityManageTypeRule::getQtrDimType,dimType);
        List<QualityManageTypeRule> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    @Override
    public List<QualityManageTypeRule> getByParam(QualityManageTypeRulePO po) {
        LambdaQueryWrapper<QualityManageTypeRule> wrapper = wrapper(po);
        List<QualityManageTypeRule> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    @Override
    public QualityManageTypeRule getByUnique(Integer dimType, Integer type, Integer subType, Long orgNo) {
        LambdaQueryWrapper<QualityManageTypeRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualityManageTypeRule::getQtrDimType,dimType)
                .eq(QualityManageTypeRule::getQtrType,type)
                .eq(QualityManageTypeRule::getQtrSubType,subType)
                .eq(QualityManageTypeRule::getQtrOrgNo,orgNo);
        return getOne(wrapper);
    }

    @Override
    public void saveOrUpdateBatch(List<QualityManageTypeRule> rules,Integer dimType) {
        QualityManageTypeRulePO po = new QualityManageTypeRulePO();
        po.setQtrDimType(dimType);
        List<QualityManageTypeRule> oldList = getByParam(po);
        List<Long> delIds = new ArrayList<>();
        for(QualityManageTypeRule rule : rules){
            QualityManageTypeRule old = getByUnique(rule.getQtrDimType(),rule.getQtrType(),rule.getQtrSubType(),rule.getQtrOrgNo());
            if(Objects.isNull(old)){
                save(rule);
            }else{
                rule.setQtrId(old.getQtrId());
                updateById(rule);
            }
        }
        //原记录多出需删除
        for(QualityManageTypeRule old : oldList){
            boolean result = rules.stream().anyMatch(
                    o -> o.getQtrDimType().equals(old.getQtrDimType())
                            && o.getQtrType().equals(old.getQtrType())
                            && o.getQtrSubType().equals(old.getQtrSubType())
                            && o.getQtrOrgNo().equals(old.getQtrOrgNo())
            );
            if(!result){
                delIds.add(old.getQtrId());
            }
        }
        removeByIds(delIds);

    }

    public LambdaQueryWrapper wrapper(QualityManageTypeRulePO po){
        LambdaQueryWrapper<QualityManageTypeRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(po.getQtrId()),QualityManageTypeRule::getQtrId,po.getQtrId())
                .eq(Objects.nonNull(po.getQtrDimType()),QualityManageTypeRule::getQtrDimType,po.getQtrDimType())
                .eq(StringUtils.isNotBlank(po.getQtrTypeName()),QualityManageTypeRule::getQtrTypeName,po.getQtrTypeName())
                .eq(Objects.nonNull(po.getQtrType()),QualityManageTypeRule::getQtrType,po.getQtrType())
                .eq(Objects.nonNull(po.getQtrSubType()),QualityManageTypeRule::getQtrSubType,po.getQtrSubType())
                .ge(Objects.nonNull(po.getQtrOrgNo()),QualityManageTypeRule::getQtrOrgNo,po.getQtrOrgNo())
                .lt(Objects.nonNull(po.getQtrAgentNo()),QualityManageTypeRule::getQtrAgentNo,po.getQtrAgentNo());
        return wrapper;
    }
}

